package com.zmn.plat.business.impl.orderconfig.returnvisitcontent;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.plat.business.interfaces.orderconfig.returnvisitcontent.ReturnVisitAnswerBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitAnswerDIO;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitAnswerDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitAnswer;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitAnswerQuery;
import com.zmn.plat.services.interfaces.orderconfig.returnvisitcontent.ReturnVisitAnswerService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description
 * @modifier
 * @since 2020/9/29 9:54
 **/
@Service
@Slf4j
public class ReturnVisitAnswerBServiceImpl implements ReturnVisitAnswerBService {
    @Resource
    ReturnVisitAnswerService returnVisitAnswerService;

    @Override
    public ReturnVisitAnswerDRO getById(Integer answerId) {
        if (Objects.isNull(answerId)) {
            log.error("answerId is null");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ReturnVisitAnswer returnVisitAnswer = returnVisitAnswerService.findById(answerId);

        if (Objects.isNull(returnVisitAnswer)) {
            log.error("returnVisitAnswer is null.answerId={}", answerId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        ReturnVisitAnswerDRO returnVisitAnswerDRO = new ReturnVisitAnswerDRO();
        BeanUtils.copyProperties(returnVisitAnswer, returnVisitAnswerDRO);
        return returnVisitAnswerDRO;
    }

    @Override
    public List<ReturnVisitAnswerDRO> listByQuery(ReturnVisitAnswerDIO returnVisitAnswerDIO) {
        if (Objects.isNull(returnVisitAnswerDIO)) {
            log.error("returnVisitAnswerDIO is null");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        if (Objects.nonNull(returnVisitAnswerDIO.getAnswerIdStr()) && CollectionUtils.isEmpty(returnVisitAnswerDIO.getAnswerIdList()) && returnVisitAnswerDIO.getAnswerIdStr().length() > 2) {
            // 去掉首尾的字符串
            String answerIdStr = returnVisitAnswerDIO.getAnswerIdStr();
            answerIdStr = answerIdStr.substring(1, answerIdStr.length() - 1);
            String[] answerIdArr = answerIdStr.split("$");
            List<Integer> answerIdList = new ArrayList<>(answerIdArr.length);
            for (int i = 0; i < answerIdArr.length; i++) {
                answerIdList.add(Integer.parseInt(answerIdArr[i]));
            }
            returnVisitAnswerDIO.setAnswerIdList(answerIdList);
        }

        ReturnVisitAnswerQuery returnVisitAnswerQuery = new ReturnVisitAnswerQuery();
        BeanUtils.copyProperties(returnVisitAnswerDIO, returnVisitAnswerQuery);
        List<ReturnVisitAnswer> returnVisitAnswerList = returnVisitAnswerService.listByQuery(returnVisitAnswerQuery);

        if (CollectionUtil.isNullOrEmpty(returnVisitAnswerList)) {
            log.error("returnVisitAnswer is null.returnVisitAnswerDIO={}", returnVisitAnswerDIO);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        List<ReturnVisitAnswerDRO> dros = returnVisitAnswerList.stream().map(returnVisitAnswer -> {
            ReturnVisitAnswerDRO returnVisitAnswerDRO = new ReturnVisitAnswerDRO();
            BeanUtils.copyProperties(returnVisitAnswer, returnVisitAnswerDRO);
            return returnVisitAnswerDRO;
        }).collect(Collectors.toList());

        return dros;

    }
}
